#include "invert.hh"

ApplyInvert::ApplyInvert (IplImage* input_)
  : input(input_)
{
  output = cvCreateImage (cvSize (input->width, input->height), IPL_DEPTH_8U, 3);
}

void ApplyInvert::operator() (const tbb::blocked_range2d<int>& r) const
{
  for (int i = r.rows().begin(); i!= r.rows().end(); ++i)
    for (int j = r.cols().begin(); j!= r.cols().end(); ++j)
      {
	CvScalar s = cvGet2D (input, i, j);
	s.val[0] = 255 - s.val[0];
	s.val[1] = 255 - s.val[1];
	s.val[2] = 255 - s.val[2];
	cvSet2D (output, i, j, s);
      }
}

IplImage*
ApplyInvert::getOutput ()
{
  return output;
}

IplImage*
invert (IplImage* input)
{
  ApplyInvert app(input);
  tbb::parallel_for(tbb::blocked_range2d<int>(0,input->height, 0,input->width),
                    app, tbb::auto_partitioner());

  return app.getOutput ();
}
